// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// f [author] rhyme_lph
// D [email]  rhymelph@gmail.com
// k [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/provider/login/create_business_profile_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/form/value_shadow_select_form.dart';
import 'package:finance_app/src/widgets/input/input_field.dart';
import 'package:finance_app/src/widgets/login/login_top_screen_widget.dart';
import 'package:finance_app/src/widgets/pop/normal_pop.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "createBusinessProfilePage")
class CreateBusinessProfilePage extends StatefulWidget {
  @override
  _CreateBusinessProfilePageState createState() =>
      _CreateBusinessProfilePageState();
}

class _CreateBusinessProfilePageState extends State<CreateBusinessProfilePage>
    with
        PagePresenter<CreateBusinessProfilePage,
            CreateBusinessProfileProvider> {
  @override
  Widget get buildWidget => Scaffold(
        body: ListView(
          padding: EdgeInsets.zero,
          children: [
            _buildImageScreen(),
            listBuilder<bool>(
                builder: (BuildContext context, bool value, Widget child) =>
                    IndexedStack(
                      children: [
                        buildFirstScreen(),
                        buildSecondScreen(),
                      ],
                      index: value ? 1 : 0,
                    ),
                selector: (_, __) => __.showNext),
          ],
        ),
      );

  Widget buildSecondScreen() => Column(
        children: [
          AppSpaces.h35,
          _buildTipText(
            tip: S.current.more_about_your_business,
          ),
          AppSpaces.h20,
          listBuilder<DateTime>(
            selector: (_, __) => __.startDate,
            builder: (BuildContext context, DateTime value, Widget child) =>
                Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20.0),
              child: ValueShadowSelectForm(
                title: S.current.financial_year_start_date,
                value: DateTimeUtils.formatDateTime(value, 'd MMM yyyy'),
                onTap: provider.onChangeStartDate,
              ),
            ),
          ),
          AppSpaces.h20,
          listBuilder<DateTime>(
            selector: (_, __) => __.endDate,
            builder: (BuildContext context, DateTime value, Widget child) =>
                Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20.0),
              child: ValueShadowSelectForm(
                title: S.current.financial_year_end_date,
                value: DateTimeUtils.formatDateTime(value, 'd MMM yyyy'),
                onTap: provider.onChangeEndDate,
              ),
            ),
          ),
          AppSpaces.h20,
          listBuilder<String>(
              builder: (BuildContext context, String value, Widget child) =>
                  NormalPop<String>(
                    select: value,
                    buildDisplayName: (e) => e,
                    group: (Config.settingApp?.industryType ?? [])
                        .where((element) =>
                            element.businessType ==
                            provider.currentSelectBusinessType?.key)
                        .map((e) => e.name)
                        .toList(),
                    hint: S.current.industry_type,
                    onSelect: provider.onSelectIndustryType,
                  ),
              selector: (_, __) => __.currentSelectIndustryType?.name),
          AppSpaces.h30,
          listBuilder<String>(
              builder: (BuildContext context, String value, Widget child) =>
                  NormalButton(
                    title: S.current.start_exploring,
                    onTap: value != null ? provider.onStartExploring : null,
                  ),
              selector: (_, __) => __.currentSelectIndustryType?.name),
          AppSpaces.h26,
          buildStep(3),
        ],
      );

  Widget buildFirstScreen() => Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          AppSpaces.h35,
          _buildTipText(
            tip: S.current.more_about_your_business,
          ),
          AppSpaces.h20,
          InputField(
            key: ValueKey('business profile name'),
            keyboardType: TextInputType.text,
            focusNode: provider.businessNameFocusNode,
            controller: provider.businessNameController,
            tip: S.current.business_name,
          ),
          AppSpaces.h30,
          Align(
            alignment: Alignment.centerLeft,
            child: Padding(
              padding: EdgeInsets.only(left: getDp(43)),
              child: Text(
                S.current.business_type,
                style: TextStyle(
                  color: aceBlackColor,
                  fontSize: getSp(16),
                  fontWeight: FontWeight.w700,
                  height: 1,
                ),
              ),
            ),
          ),
          AppSpaces.h26,
          listBuilder<int>(
              builder: (BuildContext context, int value, Widget child) =>
                  Padding(
                    padding: EdgeInsets.only(left: getDp(43)),
                    child: Wrap(
                      alignment: WrapAlignment.start,
                      crossAxisAlignment: WrapCrossAlignment.start,
                      children: List.generate(
                          value,
                          (index) => listBuilder<Tuple2<String, bool>>(
                              builder: (BuildContext context,
                                      Tuple2<String, bool> value,
                                      Widget child) =>
                                  PressedOpacityButton(
                                    onPressed: () => provider
                                        .onChangeBusinessType(value.item1),
                                    child: NormalRadio(
                                      minWidth: getDp(125),
                                      isSelect: value.item2,
                                      title: value.item1,
                                    ),
                                  ),
                              selector: (_, __) => Tuple2(
                                  __.businessTypes[index],
                                  __.businessTypes[index] ==
                                      __.currentSelectBusinessType?.name))),
                      runSpacing: getDp(24),
                      spacing: getDp(10),
                    ),
                  ),
              selector: (_, __) => __.businessTypes.length),
          AppSpaces.h40,
          listBuilder<bool>(
              builder: (BuildContext context, bool value, Widget child) =>
                  NormalButton(
                    title: S.current.next,
                    onTap: value == true ? provider.onNext : null,
                  ),
              selector: (_, __) => __.enable),
          AppSpaces.h26,
          buildStep(2),
        ],
      );

  Widget _buildImageScreen() => LoginTopScreenWidget(
        imagePath: A.assetsImagesGraphicBusinessProfile,
        imageName: S.current.create_business_profile,
        imageBottom: 20,
      );

  Center buildStep(int index) {
    return Center(
      child: Text(
        S.current.step_index_out_of_3(index),
        style: TextStyle(
          color: aceDarkGrayColor,
          fontWeight: FontWeight.w400,
          fontSize: getSp(12),
        ),
      ),
    );
  }

  Center _buildTipText({String tip}) {
    return Center(
      child: Text(
        tip,
        style: TextStyle(
          color: aceBlackColor,
          fontSize: getSp(18),
          fontWeight: FontWeight.w700,
        ),
      ),
    );
  }
}
